package workforce.limousine.struts;

import limousine.struts.form.ProfileForm;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.util.SelectItem;
import limousine.vo.Users;
import limousine.dbservice.UsersService;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;

public class UsersEditInit extends Action {

    private Func func = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***UsersEditInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if (!user.getRole_name().equals("Admin")) {
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        session.removeAttribute(Token.USERS);
        session.removeAttribute(Token.USERS_ROLE_LIST);
        session.removeAttribute(Token.USERS_GENDER_LIST);
        session.removeAttribute(Token.USERS_ENABLE_LIST);
        session.removeAttribute(Token.USERS_DATE_LIST);
        session.removeAttribute(Token.USERS_MONTH_LIST);
        session.removeAttribute(Token.USERS_YEAR_LIST);

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        ProfileForm f = (ProfileForm) form;

        String id = request.getParameter("eid");

        UsersService us = new UsersService();
        us.setDataSource(ds);
        Users o = us.getById(id);

        if (o != null) {
            f.setId(o.getId());
            f.setUserName(o.getUserName());
            f.setName(o.getName());
            f.setPhone(o.getPhone());
            f.setEmail(o.getEmail());
            f.setCreateBy(o.getCreateBy());

            f.setPassword("");


            List<SelectItem> roleList = service.getRoleListByPrivilege(ds, user.getRoleId(), o.getRoleId(), true);
            //roleList = service.setListHaveSelectOne(roleList, o.getRoleId());
            session.setAttribute(Token.USERS_ROLE_LIST, roleList);

            List<SelectItem> genderList = service.getGenderList(o.getGender());
            //genderList = service.setListHaveSelectOne(genderList,o.getGender());
            session.setAttribute(Token.USERS_GENDER_LIST, genderList);

            List<SelectItem> enableList = service.getEnableList(o.getEnable());
            //enableList = service.setListHaveSelectOne(enableList,o.getEnable());
            session.setAttribute(Token.USERS_ENABLE_LIST, enableList);

            
        }

        return mapping.findForward(Token.SUCCESS);

    }
}
